﻿// --------------------------------
// <copyright file="DataEntryForm.cs" company="TRONSoft">
//     Copyright (c) TRONSoft.
// </copyright>
// <license>
//   This source code is subject to terms and conditions of the Microsoft
//   Public License. A copy of the license can be found in the License.html
//   file at the root of this distribution. If you cannot locate the
//   Microsoft Public License, please send an email to dlr@microsoft.com.
//   By using this source code in any fashion, you are agreeing to be bound 
//   by the terms of the Microsoft Public License. You must not remove this
//   notice, or any other, from this software.
// </license>
// <author>Tron</author>
// ---------------------------------

using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Actucal
{
    public partial class DataEntryForm : DockContent
    {
        public DataEntryForm()
        {
            InitializeComponent();

            foreach (Product product in ProductsService.Products)
            {
                lbProducts.Items.Add(product.Name);
            }
        }

        public event EventHandler CurrentProductChanged;

        protected virtual void OnCurrentProductChanged(EventArgs e)
        {
            if (null != CurrentProductChanged)
                CurrentProductChanged(this, e);
        }

        private void miAdd_Click(object sender, EventArgs e)
        {
            using (AddProductForm frm = new AddProductForm())
            {
                if (DialogResult.OK == frm.ShowDialog(this))
                {
                    Product product = new Product(frm.NameOfProduct);
                    ProductsService.Products.Add(product);

                    lbProducts.Items.Add(product.Name);
                }
            }
        }

        private void lbProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbProducts.SelectedItem != null)
            {
                ProductsService.CurrentProduct = ProductsService.GetProduct(lbProducts.SelectedItem.ToString());
                OnCurrentProductChanged(new EventArgs());
            }
        }
    }
}